/**
 * Created by zhubuqing on 2018/4/18.
 */
var searchCommonResourceUrl = baseUrl + "/common/resource/getNews";
var getNewsUrl = baseUrl + "/common/resource/getNews";

var url = decodeURI(location.href); // 获取URL

var tClassificationId = 0;

function getQueryString(url) {
    if (url.split("?").length > 1) {
        var useUrl = url.split("?")[1];
        var params = new Array();
        var needParams = new Array();
        params = useUrl.split("&");
        for (var i = 0; i < params.length; i++) {
            var needParam = params[i].split("=")[1];
            needParams.push(needParam);
        }
        return needParams;
    } else {
        return null;
    }
}

$(function () {
    // 获取专题分类ID
    tClassificationId = getQueryString(url)[0];

    // 获取浙江视频
    $("#toXqListZjsp").html('<a href="xq_list.html?type=ggzyk_zheJiangShiPin&bookId=' + 5 + '&thematicClassificationId=' + tClassificationId + '">更多</a>');
    searchCommonResourceZheJiangShiPin();

    // 获取经典文献
    $("#toXqListJdwx").html('<a href="xq_list.html?type=ggzyk_jiangDianWenXian&bookId=' + 5 + '&thematicClassificationId=' + tClassificationId + '">更多</a>');
    getNewsJingDianWenXian();

    // 获取最新
    $("#toXqListZx").html('<a href="xq_list.html?type=ggzyk_zuiXin&bookId=' + 5 + '&thematicClassificationId=' + tClassificationId + '">更多</a>');
    getNewsZuiXin();

    // 获取参阅资料
    $("#toXqListCyzl").html('<a href="xq_list.html?type=ggzyk_canYueZiLiao&bookId=' + 5 + '&thematicClassificationId=' + tClassificationId + '">更多</a>');
    getNewsCanYueZiLiao();

    // 回车事件绑定
    $('#titleSouSuo').bind('keyup', function (event) {
        if (event.keyCode == "13") {
            //回车执行查询
            self.location.href = "xq_list.html?type=titleSouSuo&keyWord=" + $("#titleSouSuo").val() + "&thematicClassificationId=" + tClassificationId;
        }
    });

    // 回车事件绑定
    $('#contentSouSuo').bind('keyup', function (event) {
        if (event.keyCode == "13") {
            //回车执行查询
            self.location.href = "xq_list.html?type=contentSouSuo&keyWord=" + $("#contentSouSuo").val() + "&thematicClassificationId=" + tClassificationId;
        }
    });
});

// 获取浙江经验
function searchCommonResourceZheJiangShiPin() {
    var data = {
        thematicClassificationId: tClassificationId,
        commonNewsTypeId: 11,
        bookId: 5,
        page: 0,
        size: 10
    };

    commonAjax(searchCommonResourceUrl, data, "searchCommonResourceZheJiangShiPinRes", "GET");
}
function searchCommonResourceZheJiangShiPinRes(res) {
    var resultData = res.obj.content;

    var commonResourceZheJiangShiPinHtml = "";

    for (var i = 0; i < resultData.length; i++) {
        if (resultData[i].field3 == "1") {
            commonResourceZheJiangShiPinHtml += '<a href="' + resultData[i].field2 + '" target="_blank"><li class="clearfix">';
        } else {
            commonResourceZheJiangShiPinHtml += '<a onclick=toXqNormal("' + resultData[i].id + '","ggzyk_zuiXin")><li class="clearfix">';
        }
        commonResourceZheJiangShiPinHtml += '<div class="pull-left News">' + resultData[i].title + '</div>';
        commonResourceZheJiangShiPinHtml += '<div class="pull-right time">' + changeTime(resultData[i].creatTime) + '</div>';
        commonResourceZheJiangShiPinHtml += '</li></a>';
    }

    $("#commonResourceZheJiangShiPin").html(commonResourceZheJiangShiPinHtml);
}

// 获取经典文献
function getNewsJingDianWenXian() {
    var data = {
        thematicClassificationId: tClassificationId,
        commonNewsTypeId: 9,
        bookId: 5,
        page: 0,
        size: 10
    };

    commonAjax(getNewsUrl, data, "getNewsJingDianWenXianRes", "GET");
}
function getNewsJingDianWenXianRes(res) {
    var resultData = res.obj.content;

    var newsJingDianWenXianHtml = "";

    for (var i = 0; i < resultData.length; i++) {
        if (resultData[i].field3 == "1") {
            newsJingDianWenXianHtml += '<a href="' + resultData[i].field2 + '" target="_blank"><li>' + resultData[i].title + '</li></a>';
        } else {
            newsJingDianWenXianHtml += '<a onclick=toXqNormal("' + resultData[i].id + '","ggzyk_jiangDianWenXian")><li>' + resultData[i].title + '</li></a>';
        }
    }

    $("#newsJingDianWenXian").html(newsJingDianWenXianHtml);
}

function getNewsZuiXin() {
    var data = {
        thematicClassificationId: tClassificationId,
        commonNewsTypeId: 10,
        bookId: 5,
        page: 0,
        size: 5
    };

    commonAjax(getNewsUrl, data, "getNewsZuiXinRes", "GET");
}
function getNewsZuiXinRes(res) {
    var resultData = res.obj.content;

    var zuiXinHtml = "";

    for (var i = 0; i < resultData.length; i++) {
        if (resultData[i].field3 == "1") {
            zuiXinHtml += '<a href="' + resultData[i].field2 + '" target="_blank"><li class="clearfix">';
        } else {
            zuiXinHtml += '<a onclick=toXqNormal("' + resultData[i].id + '","ggzyk_zuiXin")><li class="clearfix">';
        }
        zuiXinHtml += '<div class="pull-left News">' + resultData[i].title + '</div>';
        zuiXinHtml += '<div class="pull-right time">' + changeTime(resultData[i].creatTime) + '</div>';
        zuiXinHtml += '</li></a>';
    }

    $("#zuiXin").html(zuiXinHtml);
}

// 获取参阅资料
function getNewsCanYueZiLiao() {
    var data = {
        thematicClassificationId: tClassificationId,
        type: "CAN_YUE_ZI_LIAO",
        bookId: 5,
        page: 0,
        size: 10
    };

    commonAjax(baseUrl + "/common/resource/searchCommonResource", data, "getNewsCanYueZiLiaoRes", "GET");
}
function getNewsCanYueZiLiaoRes(res) {
    var resultData = res.obj.content;

    var newsCanYueZiLiaoHtml = "";

    for (var i = 0; i < resultData.length; i++) {
        newsCanYueZiLiaoHtml += '<a href="' + resultData[i].fileUrl + '" target="_blank"><li>' + resultData[i].title + '</li></a>';
    }

    $("#newsCanYueZiLiao").html(newsCanYueZiLiaoHtml);
}

// 跳转至详情
function toXqNormal(followId, type) {
    var followId = followId;
    var theType = type;
    sessionStorage.setItem("followId", JSON.stringify(followId));
    sessionStorage.setItem("type", JSON.stringify(theType));
    self.location.href = "xq.html";
}

// 跳转至视频
function toXqVideo(followId, type) {
    var followId = followId;
    var theType = type;
    sessionStorage.setItem("followId", JSON.stringify(followId));
    sessionStorage.setItem("type", JSON.stringify(theType));
    window.open("video.html");
}